<template>
    <page-main class="page-login page-login-phone">
        <div class="head">
            <h5>欢迎使用</h5>
            <h3>广医云</h3>          
            <h3>在线就医服务</h3>          
        </div>
        <p class="tip">请使用手机号登录，未注册的手机号验证后将为您自动创建账户</p>
        <van-form label-width="50" class="login-form" >
          <van-field
              v-model="form.mobile"
              placeholder="请输入您的手机号"
              size="large"
              clearable
              label="手机号"
          >
          </van-field>
          <van-field
              v-model="form.code"
              placeholder="请输入短信验证码"
              size="large"
              clearable
              label="验证码"
          >
            <template #button>
                    <send-code class="send-code" ref="refSendCode" :phone="form.mobile" @send="handleSend" />
            </template>
          </van-field>
          <div class="agreement">
                  <van-checkbox icon-size="16" checked-color="#1989fa" v-model="checked">
                    我已阅读并同意
                    <span class="c-blue" @click.stop="showAgreement('user')">《用户服务协议》</span>
                    <span class="c-blue" @click.stop="showAgreement('privacy')">《隐私权政策》</span></van-checkbox>
              </div>
          <div class="login-btn">
              <van-button type="info" round block @click="submitLogin">登 录</van-button>
          </div>
          </van-form>
          <!-- <van-submit-bar>
              <div class="agreement">
                  <van-checkbox icon-size="16" checked-color="#1989fa" v-model="checked">我已阅读并同意<span @click.stop="handleLogin('showAgreement')">《用户服务协议》</span><span @click.stop="handleLogin('showAgreement')">《隐私权政策》</span></van-checkbox>
              </div>
          </van-submit-bar> -->
          <show-agreement ref="refShowAgreement" />
    </page-main>
  </template>
  
  <script>
  import showAgreement from '@/components/ShowAgreement';
  import sendCode from '@/components/SendCode';
  import {formLoginClient} from '@/assets/js/config';
  export default {
      data(){
          return{
              icon:{
                  phone: '',
                  password: '',
              },
              checked: true,
              form:{
                  mobile: null,
                  code: null,
                  grant_type: 'mobile',
              }
          }
      },
      components:{
          showAgreement,
          sendCode
      },
      created(){
          
      },
      methods:{
        showAgreement(type){
              this.$refs.refShowAgreement.init(type);
          },
          handleSend(){
            this.$http('get', '/admin/mobile/'+ this.form.mobile, {clientId: 'app'}, (data,msg) => {
                this.$refs.refSendCode.countDown();
            }, msg => {

            })
          },
          verifyForm(){
              if(!this.$verify.mobile(this.form.mobile)){
                  return false;
              }
              if(!this.$verify.null(this.form.code, '请输入验证码')){
                  return false;
              }
              if(!this.checked){
                  this.$toast('请先阅读并同意《用户服务协议》《隐私权政策》！');
                  return false;
              }
              return true;
          },
          submitLogin(){
              if(!this.verifyForm()){
                  return;
              }
              let opt = JSON.parse(JSON.stringify(this.form));
              opt.mobile = 'VIPSMS@' + opt.mobile;
              let str = this.$util.serialize(opt);
              this.$http('post', '/auth/oauth/token'+ str, {}, (data,msg) => { 
                if(data.data.code == 1){
                    this.$dialog.alert({message: data.data.msg});
                    return;
                }
                  localStorage.setItem('access_token', data.data.access_token);
                  this.$toast.success('登录成功！');
                //   this.$http('get', '/lgg/agency/info',{},(data,msg) => {
                //       localStorage.setItem('userInfo', JSON.stringify(data.data));
                //       if(data.data.wxBinding){ //判断是否已绑定微信
                //           this.$store.commit('setUserInfo', data.data);
                //           if(data.data.completeFlag == '0'){
                              this.$util.gotoPage('/');
                //           }else{
                //               this.$util.gotoPage('/user/perfect', data.data);
                //           }
                //       }else{
                //           this.$util.gotoPage('/login/bind', this.form);
                //       }
                //   });
              }, msg => {
                
              }, {headers:{Authorization : 'Basic ' + window.btoa(formLoginClient)}}, 'all')            
          },
  
      }
  }
  </script>
  
  <style>
  
  </style>